package org.petstore.web.servlet;

import org.petstore.domain.Account;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

import static org.petstore.web.servlet.util.ServletUtil.LOGIN_FORM;

public class MyAccountFormServlet extends HttpServlet {
    private static final String MY_ACCOUNT_FORM = "/WEB-INF/jsp/account/IncludeAccountFields.jsp";

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();

        Account loginUser = (Account) session.getAttribute("loginUser");
        if (loginUser == null) {
            request.setAttribute("msg", "请先登录");
            request.getRequestDispatcher(LOGIN_FORM).forward(request, response);
        }
        request.getRequestDispatcher(MY_ACCOUNT_FORM).forward(request, response);
    }
}
